package com.kdtech.analyse.blog;
import com.kdtech.crawler.CrawlHTML;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * 凤凰网博客解析
 * @author KK
 *
 */
public class IfengBlogAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://[a-z0-9]*.blog.ifeng.com[/]+article/[0-9]*.html",
				"http://blog.ifeng.com[/]+article/[0-9]*.html",
				"http://blog.tw.ifeng.com[/]+article/[0-9]*.html",
				};
		return RegexUtils.matchAny(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		String title = null;
		String content = null;
		Long date = null;
		String dateStr = null;
		String commnetNum = null;
		String clickNum = null;
		String deliverNum = null;
		String updateUrl = url;
		String author=null;
		Document doc = Jsoup.parse(htmltxt);
		author = doc.select("div#blog_main_right_pic p strong").text();
		title = doc.select("title").text();
		if (StringUtils.isNotEmpty(title)){
			title = StringUtils.substringBeforeLast(title, "--");
			title = StringUtils.substringBeforeLast(title, "--");
		}

		dateStr = doc.select("div#blog_main_left div.blog_main_time").text();
		doc.select("div#blog_main_right_link").remove();
		doc.select("div#blog_article_content ul").remove();
		content = HtmlCleaner.getContentHtml(url,  doc.select("div#blog_article_content p"));

		String contentUrl = doc.select("div#blog_article_content > script").attr("src");
		if (StringUtils.isNotBlank(contentUrl)){
			String html = CrawlHTML.GetHtml(contentUrl, "UTF-8");
			if (StringUtils.isNotBlank(html)){
				html = StringUtils.substringAfter(html, "message\":\"");
				html = StringUtils.substringBefore(html, ",\"");
				html = html.replace("\\/", "/");

//				html = HttpUtils.htmlUnescape(html);
				Document parse = Jsoup.parse(html);
				content = StringUtils.decodeUnicode(parse.html(), true);
			}
		}
		String text = doc.select("div#blog_main_left div.blog_main_time").text();
		if (StringUtils.isNotBlank(text)){
			clickNum =StringUtils.substringBetween(text, "浏览","次");
			commnetNum =StringUtils.substringBetween(text, "| 评论 ","条");
		}
		date = DateUtils.matchDate(dateStr);
		if (date==null){
			date = DateUtils.matchDate(dateStr);
		}

			NewsMeta news = new NewsMeta();
			news.setUrl(url);
			news.setType(7);
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(NumberUtils.parseInt(commnetNum));
			news.setClickNum(NumberUtils.parseInt(clickNum));
//			news.setDeliverNum(NumberUtils.parseInt(deliverNum));
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);
			news.setAuthor(author);
			news.setUpdateUrl(updateUrl);
			return news;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if (meta!=null &&StringUtils.isNotBlank(meta.getUpdateUrl())){
			UrlMeta urlMeta = CrawlHTML.responseToURL(meta.getUpdateUrl());
			if (urlMeta!=null && urlMeta.getHtml()!=null){
				return parserHtml(urlMeta);
			}
		}
		return null;
	}



	public static void main(String[] args) {
		String url = "http://blog.ifeng.com/article/33061045.html";
		IfengBlogAnalyse test = new IfengBlogAnalyse();
		if(test.isDetailPage(url)){
			UrlMeta responseToURL = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = test.parserHtml(responseToURL);
			System.out.println(parserHtml);
		}else{
			System.out.println("不符合规则");
		}
	}

	
}
